﻿using System;
using System.Web.UI.WebControls;
using Cvk.Staffs;

namespace BatDongSan.Manager
{
    public partial class ManStaffGroupContacts : System.Web.UI.Page
    {
        public static int _groupId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _groupId = Convert.ToInt32(Request.QueryString["groupid"]);
                LoadStaffs(_groupId);
            }
        }
        public void LoadStaffs(int groupId)
        {
            try
            {
                grvStaff.DataSource = StaffBll.VGStaffContact.GetList(groupId, Utility.strConnect);
                grvStaff.DataBind();
            }
            catch { }
        }

        protected void grvStaff_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    VG_Staff_Contact info = (VG_Staff_Contact)e.Row.DataItem;

                    Label lblId = (Label)e.Row.FindControl("lblStaffId");

                    TextBox txtPosition = (TextBox)e.Row.FindControl("txtPosition");
                    txtPosition.Text = info.Position.ToString();

                    LinkButton lnkbtDenied = (LinkButton)e.Row.FindControl("lnkbtDenied");
                    lnkbtDenied.Text = "Huỷ";
                    lnkbtDenied.ForeColor = System.Drawing.Color.Red;
                    lnkbtDenied.Attributes.Add("id", info.StaffId.ToString());
                }
            }
            catch { }
        }
        protected void lnkbtDenied_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton lnkbtDenied = (LinkButton)sender;
                int id = Convert.ToInt32(lnkbtDenied.Attributes["id"]);
                if (id > 0)
                {
                    Cvk_Contact_GroupStaff info = new Cvk_Contact_GroupStaff();
                    info.GroupId = _groupId;
                    info.StaffId = id;
                    if (StaffBll.ContactGroupStaff.Delete(info, Utility.strConnect))
                    {
                        lbStatus.Text = "Bạn đã Huỷ thành viên ra khỏi nhóm thành công";
                        LoadStaffs(_groupId);
                    }
                    else { lbStatus.Text = "Lỗi, Không thể thực hiện thao tác này."; }
                }
            }
            catch { }
        }
        protected void lnkbtActive_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton lnkbtActive = (LinkButton)sender;
                int id = Convert.ToInt32(lnkbtActive.Attributes["id"]);
                if (id > 0)
                {
                    Cvk_Contact_GroupStaff info = new Cvk_Contact_GroupStaff();
                    info.GroupId = _groupId;
                    info.StaffId = id;
                    if (StaffBll.ContactGroupStaff.Insert(info, Utility.strConnect))
                    {
                        lbStatus.Text = "Bạn đã Thêm thành viên vào nhóm thành công";
                        LoadStaffs(_groupId);
                    }
                    else { lbStatus.Text = "Lỗi, Không thể thực hiện thao tác này."; }
                }
            }
            catch { }
        }
        protected void btnPosition_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in grvStaff.Rows)
                {
                    Label lbId = (Label)row.FindControl("lblStaffId");
                    int id = Convert.ToInt32(lbId.Text);
                    TextBox txt = (TextBox)row.FindControl("txtPosition");
                    int pos = Convert.ToInt32(txt.Text);
                    Cvk_Contact_GroupStaff info = new Cvk_Contact_GroupStaff();
                    info.GroupId = _groupId;
                    info.StaffId = id;
                    info.Position = pos;
                    StaffBll.ContactGroupStaff.UpdatePosition(info, Utility.strConnect);
                }
                LoadStaffs(_groupId);
            }
            catch (Exception ex)
            {
                lbStatus.Text = ex.ToString();
            }
        }
    }
}
